<?php

class App_Controllers_albums
{
    private $_template;
    private $_models = array();

    public function index()
    {
        $this->_models['albums'] = new App_Models_Albums();

        $this->_template = new Core_Template();

        $this->_template->add_css_file(array('/App/Views/index.css',
                                             '/App/Views/albums/albums.css'
                                       ));

        $this->_template->add_js_file(array('/App/Views/js/jquery.js',
                                        '/App/Views/js/albums.js'
        								
                                ));



         $this->_template->get_smarty()->assign('js_file',$this->_template->get_js_file());



        $this->_template->set_title("Фотогалерея");

        $this->_template->get_smarty()->assign('title',$this->_template->get_title());
        $this->_template->get_smarty()->assign('css_file',$this->_template->get_css_file());

        $this->_template->get_smarty()->assign('content','albums/albums');
        $this->_template->get_smarty()->assign('albums',$this->_models['albums']->get_albums());
        $this->_template->get_smarty()->display('index.tpl');
    }

    public function view(Core_Request $request)
    {
       

        $this->_models['photos'] = new App_Models_Photo();
        $this->_models['albums'] = new App_Models_Albums();

        $photos = $this->_models['photos']->get_albums_photos($request->params['id']);
 
        $this->_template = new Core_Template();

        $this->_template->add_css_file(array('/App/Views/index.css',
                                             '/App/Views/albums/albums.css',
        									 '/App/Views/comment/comment.css'
                                       ));
                                       
		$this->_template->add_js_file(array(
						'/App/Views/js/ajax.js',
						'/App/Views/js/jquery.js',
						'/App/Views/js/jquery.hoverpulse.js',
						'/App/Views/js/view.js',
						'/App/Views/js/jquery.blockUI.js',
						'/App/Views/js/photo.js',
						'/App/Views/js/comment.js',
						'/App/Views/js/jquery.corner.js'
		));



        $this->_template->get_smarty()->assign('js_file',$this->_template->get_js_file());
;
        $album = $this->_models['albums']->get_album($request->params['id']);

        if (false === $album) {
            $this->_template->set_title("not found");
            $this->_template->error_page();
        } else {
            $this->_template->set_title($album[0]['name']);
        }

        $this->_template->get_smarty()->assign('title',$this->_template->get_title());
        $this->_template->get_smarty()->assign('css_file',$this->_template->get_css_file());


        $this->_template->get_smarty()->assign('content','albums/photos');
        //$this->_template->get_smarty()->assign('albums',$this->_models['albums']->get_albums());


        $this->_template->get_smarty()->assign('albums_name',$album[0]['id']);
        $this->_template->get_smarty()->assign('aid',$album[0]['id']);
        $this->_template->get_smarty()->assign('photo',$photos);
       
        /*
        if (false === $photos) {

        } else {

        }*/
        $this->_template->get_smarty()->display('index.tpl');
    }

    public function photo(Core_Request $request)
    {

        $this->_template = new Core_Template();

        if (!isset($request->params['id'])) {
            $this->_template->error_page();
        }

        $this->_models['photos'] = new App_Models_Photo();
        $this->_models['albums'] = new App_Models_Albums();
        $this->_models['comment'] = new App_Models_Comment();

        $photos = $this->_models['photos']->get_albums_photo($request->params['id']);

        $this->_template->add_css_file(array('/App/Views/index.css',
                                             '/App/Views/albums/albums.css',
                                             '/App/Views/comment/comment.css'
                                       ));

        $this->_template->add_js_file(array(
        				'/App/Views/js/ajax.js',
        				'/App/Views/js/jquery.js',
        				'/App/Views/js/photo.js',
        				'/App/Views/js/comment.js',
        				'/App/Views/js/jquery.corner.js'
        ));

       

        $this->_template->get_smarty()->assign('js_file',$this->_template->get_js_file());

        $this->_template->get_smarty()->assign('title','');
        $this->_template->get_smarty()->assign('css_file',$this->_template->get_css_file());

        
        $this->_template->get_smarty()->assign('content','albums/photo');
        //$this->_template->get_smarty()->assign('albums',$this->_models['albums']->get_albums());


        $this->_template->get_smarty()->assign('albums',$photos[1]);
        
        $al_photos = $this->_models['photos']->get_albums_photos($photos[1]['id']);
        
        $id_photo = $photos[0]['id'];
        
        $photo_index = $this->_models['photos']->get_index($al_photos,$id_photo);

        $this->_models['photos']->set_index(count($al_photos),$photo_index,$prev,$next);

        $this->_template->get_smarty()->assign('id_prev',$al_photos[$prev]['id']);
        $this->_template->get_smarty()->assign('id_next',$al_photos[$next]['id']);

        $this->_template->get_smarty()->assign('photo',$photos[0]);

        $comments = $this->_models['comment']->get_comments("fotocomment","photo_id",$request->params['id']);
        
        $this->_template->get_smarty()->assign('comments',$comments);

        $this->_template->get_smarty()->display('index.tpl');
    }

    public function add_comment()
    {

        if ((isset($_POST['author'])) && (isset($_POST['text'])) && (isset($_POST['email']))) {
            if ((!empty($_POST['author'])) && (!empty($_POST['text'])) && (!empty($_POST['email']))) {
                $this->_models['comment'] = new App_Models_Comment();
                $this->_models['comment']->add_comment("fotocomment","photo_id",$_GET['id'], $_POST['author'], $_POST['email'], $_POST['text']);

                echo  $_POST['author']."|#|". date("Y-m-d H:i:s")."|#|". $_POST['text'];
            } else {
                echo "1|#|none|#|2";
            }
        }

    }

    public function __call($name, $args)
    {
        $this->_template = new Core_Template();
        $this->_template->error_page();
    }
}
?>
